//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace OpenLS.UI
{
    class TwoLineControl: ItemsControl
    {
        public TwoLineControl()
        {
            var factory = new FrameworkElementFactory(typeof (TwoLinePanel));
          //  factory.SetBinding(TwoLinePanel.UsesTwoLinesProperty, new Binding("UsesTwoLines"){RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor){AncestorType =  typeof (TwoLineControl)}});
            ItemsPanel = new ItemsPanelTemplate(factory);
        }


        public bool UsesTwoLines
        {
            get { return (bool)GetValue(UsesTwoLinesProperty); }
            set { SetValue(UsesTwoLinesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for UsesTwoLines.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UsesTwoLinesProperty =
            DependencyProperty.Register("UsesTwoLines", typeof(bool), typeof(TwoLineControl), new FrameworkPropertyMetadata(false
                , FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure, onUsesTwoLinesChanged));

        private static void onUsesTwoLinesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue == e.NewValue)
                return;
            TwoLineControl c = (TwoLineControl) d;
            if (VisualTreeHelper.GetChildrenCount(c) == 0)
                return;
            Border border = (Border)VisualTreeHelper.GetChild(c, 0);
            ItemsPresenter presenter = (ItemsPresenter) VisualTreeHelper.GetChild(border, 0);
            TwoLinePanel panel = (TwoLinePanel)VisualTreeHelper.GetChild(presenter, 0);
            panel.InvalidateMeasure();
        }


        public object Content
        {
            get { return GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(object), typeof(TwoLineControl), 
            new FrameworkPropertyMetadata(null,new PropertyChangedCallback(onMyPropertyChanged)));

        private static void onMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue == e.NewValue)
                return;
            
            var control = (TwoLineControl) d;
            object o1 = control.Content;
            object o2 = control.Symbol;
            var items = new List<object>();
            if (o1 is string)
            {
                string[] ss = ((string) o1).Split(' ');
                foreach (var s in ss)
                {
                    items.Add(s + " ");
                }
            }
            else if (o1 != null)
                items.Add(o1);
            if (o2 != null)
                items.Add(o2);
            control.ItemsSource = items;
        }


        public object Symbol
        {
            get { return GetValue(SymbolProperty); }
            set { SetValue(SymbolProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Symbol.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SymbolProperty =
            DependencyProperty.Register("Symbol", typeof(object), typeof(TwoLineControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(onMyPropertyChanged)));






    }
}
